<?php

class Admin_con extends CI_Controller {

    function __construct() {
        parent::__construct();
        //cek session
        if(!$this->session->userdata('islogin')){
            redirect(base_url().'progress/login_con/admin_login');
        }
    }
    
    function index() {
        $this->load->view('progress/admin/header_view');
        $this->load->view('progress/admin/footer_view');
        
    }
    
    function list_divisi(){
        $display=array();
        $query = $this->db->query('select * from list_divisi')->result_array();
        $display['divisi'] = $query;
        $this->load->view('progress/admin/header_view');
        $this->load->view('progress/admin/divisi_list_view', $display);
        $this->load->view('progress/admin/footer_view');        
    }
    
    function tambah_divisi(){
        $this->load->view('progress/admin/header_view');
        $this->load->view('progress/admin/divisi_tambah_view');
        $this->load->view('progress/admin/footer_view');        
    }

    function proses_tambah_divisi(){
        if(!empty($_POST['divisi'])){
            $nama = $_POST['divisi'];
            $query = $this->db->query('select * from list_divisi where NAMA_DIVISI="'.$nama.'"')->result_array();
            if(count($query)<=0){
                $data = array(
                    'NAMA_DIVISI'=>$nama
                );
                if($this->db->insert('list_divisi', $data)){
                    redirect('progress/admin_con/list_divisi');
                }
            }
        }
        redirect(base_url().'progress/admin_con');
    }
    
    function edit_divisi($id){
        $query = $this->db->query('select * from list_divisi where ID_DIVISI="'.$id.'"')->result_array();
        $display = array();
        if(count($query)>0) $display['divisi'] = $query[0];
        $this->load->view('progress/admin/header_view');
        $this->load->view('progress/admin/divisi_edit_view', $display);
        $this->load->view('progress/admin/footer_view');        
    }

    function proses_edit_divisi($id){
        if(!empty($_POST['divisi'])){
            $nama = $_POST['divisi'];
            $data = array(
                'NAMA_DIVISI'=>$nama
            );
            $this->db->where('ID_DIVISI', $id);
            if($this->db->update('list_divisi', $data)){
                redirect('progress/admin_con/list_divisi');
            }
        }
        redirect(base_url().'progress/admin_con');
    }
    
    function delete_divisi($id){
        $query = $this->db->query('select * from list_divisi where ID_DIVISI="'.$id.'"')->result_array();
        if(count($query)>0){
            $this->db->where('ID_DIVISI', $id);
            if($this->db->delete('list_divisi')){
            }
        }
        redirect('progress/admin_con/list_divisi');
    }
    
    function list_non_user(){
        $query = $this->db->query('select * from user where ID not in (select ID_USER from progress_user)')->result_array();
        $display=array();
        $display['user'] = $query;
        $this->load->view('progress/admin/header_view');
        $this->load->view('progress/admin/user_non_list_view', $display);
        $this->load->view('progress/admin/footer_view');                
    }
    
    function daftarkan_user(){
        if(!empty($_POST['list_check'])){
            $list = $_POST['list_check'];
            foreach ($list as $value) {
                $data = array(
                    'ID_USER'=>$value,
                    'ROLE' =>'user'
                );
                $this->db->insert('progress_user', $data);
            }
        }
        redirect (base_url().'progress/admin_con/list_non_user');
    }
    
    
    function list_user(){
        $query = $this->db->query('select b.ID as ID_USER,b.NAMA as NAMA from progress_user as a inner join user as b on a.ID_USER=b.ID')->result_array();
        $display=array();
        $display['user'] = $query;
        $this->load->view('progress/admin/header_view');
        $this->load->view('progress/admin/user_list_view', $display);
        $this->load->view('progress/admin/footer_view');                
    }
    
    function hapus_user(){
        if(!empty($_POST['list_check'])){
            $list = $_POST['list_check'];
            foreach ($list as $value) {
                $this->db->where('ID_USER', $value);
                $this->db->delete('progress_user');                    
            }
        }
        redirect (base_url().'progress/admin_con/list_user');
    }
}

?>
